/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
function Calendar() {

}
Calendar.prototype.plusChange = function(sdiv) {
    var _dd = document.getElementById("calendar_date").value;
    var _mm = document.getElementById("calendar_month").value;
    var _yy = document.getElementById("calendar_year").value;
    var iy = parseInt(_yy);
    var im = parseInt(_mm) +2;
    if( parseInt(_mm) == 11 ) {
        iy = iy + 1;
        im = 1;
    }
    this.show(sdiv, ""+iy+"-"+format2N(im)+"-"+format2N(_dd)+" 00:00:00");
}
Calendar.prototype.minusChange = function(sdiv) {
    var _dd = document.getElementById("calendar_date").value;
    var _mm = document.getElementById("calendar_month").value;
    var _yy = document.getElementById("calendar_year").value;
    var iy = parseInt(_yy);
    var im = parseInt(_mm);
    if( parseInt(_mm) == 0 ) {
        iy = iy - 1;
        im = 12;
    }
    this.show(sdiv, ""+iy+"-"+format2N(im)+"-"+format2N(_dd)+" 00:00:00");
}
Calendar.prototype.showChange = function(sdiv) {
    var _dd = document.getElementById("calendar_date").value;
    var _mm = document.getElementById("calendar_month").value;
    var _yy = document.getElementById("calendar_year").value;
    var im = parseInt(_mm) +1;
    //alert("month:["+ im+"]");
    this.show(sdiv, ""+_yy+"-"+format2N(im)+"-"+format2N(_dd)+" 00:00:00");
}
Calendar.prototype.showMe = function(sdiv) {
    var sdate = document.getElementById(sdiv).value;
    this.show(sdiv, sdate);
}
Calendar.prototype.show = function(sdiv,src) {
    var doc = document.getElementById("calendar_popup");
    var date = new Date();
    var mm = date.getMonth();
    var yy = date.getFullYear();
    var date_now = date.getDate();

    if( src != null ) {
        if( src != "" ) {
            var sds = src.split(" ");
            var yfull = sds[0].split("-");
            if( yfull[1].substr(0, 1) == "0" ) {
                mm = (parseInt(yfull[1].substr(1, yfull[1].length)) - 1);
            } else {
                mm = (parseInt(yfull[1]) - 1);
            }
            if( yfull[2].substr(0, 1) == "0" ) {
                date_now = (parseInt(yfull[2].substr(1, yfull[2].length)));
            } else {
                date_now = (parseInt(yfull[2]));
            }
            yy = parseInt(yfull[0]);
            date.setDate(date_now);
            date.setMonth(mm, date_now);
            date.setYear(yy);
        }
    }

    var weeks = new Array();
    var week = new Array();
    
    var months = "JANUARY,FEBUARY,MACH,APIL,MAY,JUNE,JULY,AUGUST,SEBTEMBER,OCTOBER,NOVEMBER,DECEMBER".split(",");
    var weekLabel = "Sun,Mon,Tue,Wed,Thu,Fri,Sat".split(",");
    
    date.setDate(1);
    var day = date.getDay();

    var blastOfMonth = 0;
    if( mm == 0 ) {
        blastOfMonth = this.getLastDayOfMonth(yy-1, 11);
    } else {
        blastOfMonth = this.getLastDayOfMonth(yy, mm-1);
    }
    var alastOfMonth = this.getLastDayOfMonth(yy, mm);
    var numb = (7 -( 7 - day));
    var i = 0, j = 0;
    if( numb > 0 ) {
        var dl = blastOfMonth - numb;
        for(i = 0, j = (dl+1); j <= blastOfMonth; i++, j++) {
            week[i] = j;
        }
        for(i = numb, j = 1; i < 7; i++, j++ ){
            week[i] = j;
        }
    } else {
        for(i = 0, j = 1; i < 7; i++, j++) {
            week[i] = j;
        }
    }
    weeks[0]=week;
    i = 0;
    var wcount = 1;
    week = new Array();
    while( j <= alastOfMonth ) {
        week[i] = j;
        if( i == 6 ) {
            weeks[wcount] = week;
            i = 0;
            wcount++;
            week = new Array();
        } else { 
            i++;
        }
        j++;
    }
    if( i < 7 )
    {
        j = 1;
        while( i < 7 ) {
            week[i] = j;
            i++;
            j++;
        }
        weeks[wcount] = week;
        wcount++;
    }
    //    if( j <= 7 ) {
    //        week = new Array();
    //        for(i = 0; i < 7; j++, i++) {
    //            week[i] = j;
    //        }
    //        weeks[wcount] = week;
    //    }
    var buf = new StringBuffer();
    buf.append("<input type=\"hidden\" id=\"calendar_date\" name=\"calendar_date\" value=\""+date_now+"\">");
    buf.append("<table class=\"\">");
    buf.append("<tr>");
    buf.append("<td class=\"calendar_header\" colspan=\"7\" align=\"center\">");
    buf.append("<img src=\"../imgs/left_16.png\" onmouseover=\"icon_point(this)\" onclick=\"changeMinusCalendar('"+sdiv+"')\">&nbsp;&nbsp;");
    buf.append("<select id=\"calendar_month\" onchange=\"changeCalendar('"+sdiv+"')\">");
    for (i = 0; i < months.length; i++) {
        if( mm == i ) {
            buf.append("<option value='"+i+"' selected>"+ months[i]+"</option>");
        } else {
            buf.append("<option value='"+i+"'>"+ months[i]+"</option>");
        }
    }
    buf.append("</select>");
    buf.append("<select id=\"calendar_year\" onchange=\"changeCalendar('"+sdiv+"')\">");
    for (i = 2000; i <= 2050; i++) {
        if( yy == i ) {
            buf.append("<option value='"+i+"' selected>"+i+"</option>");
        } else {
            buf.append("<option value='"+i+"'>"+i+"</option>");
        }
    }
    buf.append("</select>");
    buf.append("&nbsp;&nbsp;<img src=\"../imgs/right_16.png\" onmouseover=\"icon_point(this)\" onclick=\"changePlusCalendar('"+sdiv+"')\">");
    buf.append("</td>");
    buf.append("</tr>");
    buf.append("<tr class=\"calendar_week_bar\">");
    for( i = 0; i < weekLabel.length; i++) {
        buf.append("<td align=\"center\" class=\"calendar_week_bar\">"+ weekLabel[i]+"</td>");
    }
    buf.append("</tr>");
    var flag = true;
    for(i=0;i<weeks.length;i++) {
        var dw = weeks[i];
        buf.append("<tr>");
        for(j=0;j<dw.length;j++) {
            if( dw[j] == 1 ) {
                flag = (flag == true) ? false : true;
            }
            if( j == 0 || j == 6 ) {
                buf.append("<td align=\"center\"");            
                if( flag == false ) {
                    if( date_now == dw[j] ) {
                        buf.append("class=\"calendar_day_now\">");
                        buf.append(""+ dw[j]+"");
                    } else {
                        buf.append("class=\"calendar_day_nowork\">");
                        buf.append("<a href=\"javascript:void(0)\" onclick=\"getDateField('"+sdiv+"',"+dw[j]+")\">"+ dw[j]+"</a>");
                    }
                } else {
                    buf.append("class=\"calendar_day_nowork_no\">");
                    buf.append(""+ dw[j]+"");
                }
                buf.append("</td>");
            } else {
                buf.append("<td align=\"center\"");

                if( flag == false ) {
                    if( date_now == dw[j] ) {
                        buf.append("class=\"calendar_day_now\">");
                        buf.append(""+ dw[j]+"");
                    } else {
                        buf.append("class=\"calendar_day_normal\">");
                        buf.append("<a href=\"javascript:void(0)\" onclick=\"getDateField('"+sdiv+"',"+dw[j]+")\">"+ dw[j]+"</a>");
                    }
                } else {
                    buf.append("class=\"calendar_day_normal_no\">");
                    buf.append(""+ dw[j]+"");
                }
                buf.append("</td>");
            }
        }
        buf.append("</tr>");
    }
    buf.append("<tr>");
    buf.append("<td class=\"calendar_footer\" colspan=\"7\" align=\"center\">&nbsp;");
    var text = "";
    buf.append("<select id=\"calendar_hour\" name=\"calendar_hour\">");
    for(i=0;i<24;i++) {
        text = ""+i;
        if( text.length == 1) {
            text = "0"+text;
        }
        //        if( date.getHours() == i ) {
        //            buf.append("<option value=\""+i+"\" selected>"+ i+"</option>");
        //        } else {
        buf.append("<option value=\""+i+"\">"+ text+"</option>");
    // }
    }
    buf.append("</select>");
    buf.append("<select id=\"calendar_minute\" name=\"calendar_minute\">");
    for(i=0;i<60;i++) {
        text = ""+i;
        if( text.length == 1) {
            text = "0"+text;
        }
        //        if( date.getMinutes() == i ) {
        //            buf.append("<option value=\""+i+"\" selected>"+ text+"</option>");
        //        } else {
        buf.append("<option value=\""+i+"\">"+text+"</option>");
    // }
    }
    buf.append("</select>");
    buf.append("<select id=\"calendar_second\" name=\"calendar_second\">");
    for(i=0;i<60;i++) {
        text = ""+i;
        if( text.length == 1) {
            text = "0"+text;
        }
        buf.append("<option value=\""+i+"\">"+text+"</option>");
    }
    buf.append("</select>");
    buf.append("<input type=\"button\" name=\"todate\" value=\"TODATE\" onclick=\"getDateField('"+sdiv+"',"+date_now+")\">");
    buf.append("</td>");
    buf.append("</tr>");
    buf.append("</table>");
    doc.innerHTML = buf.toString();
}
Calendar.prototype.getLastDayOfMonth = function(year, month)
{
    var day;
    var _month = month + 1;
    switch(_month)
    {
        case 1 :
        case 3 :
        case 5 :
        case 7 :
        case 8 :
        case 10:
        case 12:
            day = 31;
            break;
        case 4 :
        case 6 :
        case 9 :
        case 11:
            day = 30;
            break;
        case 2 :
            if( ( ((year % 4) == 0) && ( (year % 100) != 0) ) || ((year % 400) == 0) )
                day = 29;
            else
                day = 28;
            break;
    }
    return day;

}
function format2N(number) {
    var txt = ""+number;
    if( txt.length == 1 ) {
        txt = "0"+ txt;
    }
    return txt;
}
function getDateField(sid,dd) {
    var year = document.getElementById("calendar_year").value;
    var month = document.getElementById("calendar_month").value;
    var hour = document.getElementById("calendar_hour").value;
    var minute = document.getElementById("calendar_minute").value;
    var second = document.getElementById("calendar_second").value;
    var time = format2N(hour)+":"+format2N(minute)+":"+format2N(second);
    var im = parseInt(month) + 1;
    document.getElementById(sid).value = ""+year+"-"+format2N(im)+"-"+format2N(dd)+" "+time;
    hiddenCalendar();
}
function changePlusCalendar(sName) {
    var div_cal = document.getElementById("div_calendar_popup");
    var divcal = document.getElementById("calendar_popup");
    var calendar = new Calendar();
    calendar.plusChange(sName);
    var width =  window.innerWidth;
    var height = window.innerHeight;
    divcal.style.width = 300+"px";
    divcal.style.height= 280+"px";
    var left2 = ((width/2) - (300/2));
    var top2 = ((height/2) - (280/2));
    var left = 0;
    var top = 0;
    div_cal.style.visibility = "visible";
    divcal.style.borderStyle = "ridge";
    divcal.style.borderWidth = "medium";
    div_cal.style.height = height+"px";
    div_cal.style.width = width +"px";
    div_cal.style.top = left + "px";
    div_cal.style.left = top  + "px"
    divcal.style.top = top2+"px";
    divcal.style.left = left2+"px";
    divcal.style.visibility = "visible";
}
function changeMinusCalendar(sName) {
    var div_cal = document.getElementById("div_calendar_popup");
    var divcal = document.getElementById("calendar_popup");
    var calendar = new Calendar();
    calendar.minusChange(sName);
    var width =  window.innerWidth;
    var height = window.innerHeight;
    divcal.style.width = 300+"px";
    divcal.style.height= 280+"px";
    var left2 = ((width/2) - (300/2));
    var top2 = ((height/2) - (280/2));
    var left = 0;
    var top = 0;
    div_cal.style.visibility = "visible";
    divcal.style.borderStyle = "ridge";
    divcal.style.borderWidth = "medium";
    div_cal.style.height = height+"px";
    div_cal.style.width = width +"px";
    div_cal.style.top = left + "px";
    div_cal.style.left = top  + "px"
    divcal.style.top = top2+"px";
    divcal.style.left = left2+"px";
    divcal.style.visibility = "visible";
}
function changeCalendar(sName) {
    var div_cal = document.getElementById("div_calendar_popup");
    var divcal = document.getElementById("calendar_popup");
    var calendar = new Calendar();
    calendar.showChange(sName);
    var width =  window.innerWidth;
    var height = window.innerHeight;
    divcal.style.width = 300+"px";
    divcal.style.height= 280+"px";
    var left2 = ((width/2) - (300/2));
    var top2 = ((height/2) - (280/2));
    var left = 0;
    var top = 0;
    div_cal.style.visibility = "visible";
    divcal.style.borderStyle = "ridge";
    divcal.style.borderWidth = "medium";
    div_cal.style.height = height+"px";
    div_cal.style.width = width +"px";
    div_cal.style.top = left + "px";
    div_cal.style.left = top  + "px"
    divcal.style.top = top2+"px";
    divcal.style.left = left2+"px";
    divcal.style.visibility = "visible";
}
function calendarShow(sdiv) {
    var div_cal = document.getElementById("div_calendar_popup");
    var divcal = document.getElementById("calendar_popup");
    var calendar =new Calendar();
    calendar.showMe(sdiv);
    var width =  window.innerWidth;
    var height = window.innerHeight;
    divcal.style.width = 300+"px";
    divcal.style.height= 280+"px";
    var left2 = ((width/2) - (300/2));
    var top2 = ((height/2) - (280/2));
    var left = 0;
    var top = 0;
    div_cal.style.visibility = "visible";
    divcal.style.borderStyle = "ridge";
    divcal.style.backgroundColor = "#6699CC";
    divcal.style.borderWidth = "medium";
    div_cal.style.height = height+"px";
    div_cal.style.width = width +"px";
    div_cal.style.top = left + "px";
    div_cal.style.left = top  + "px"
    divcal.style.top = top2+"px";
    divcal.style.left = left2+"px";
    divcal.style.visibility = "visible";
}
function hiddenCalendar() {
    var divcal = document.getElementById("calendar_popup");
    divcal.style.visibility = "hidden";
    var div_cal = document.getElementById("div_calendar_popup");
    div_cal.style.visibility = "hidden";
}


